/*
 * ------------------------------------------------------------------
 * 初始化中间件
 * ------------------------------------------------------------------
 * 此中间件用于为后续中间件以及接口提供必要的参数
 * ------------------------------------------------------------------
 * @关于我：
 * QQ:2815473064
 * wechat:13531609457
 * ------------------------------------------------------------------
 */
const Config = require("../../config");
const userAgentModule = require("../../utils/userAgent");
const DayJs = require("dayjs");
const Address6 = require("ip-address").Address6;
module.exports = (request, res, next) => {
  try {
    let { path, ip, method, httpVersion } = request;
    let SecretKeyConfig = Config.SecretKey;
    let dateNow = DayJs();
    // 当前时间戳
    let currentTime = dateNow.valueOf();
    let userAddress = new Address6(ip).inspectTeredo();
    // 当前请求地址
    let pathname = path;
    // 请求IP地址
    let IPV4 = userAddress.client4;
    // 请求IP地址
    let IPV6 = userAddress.client6;
    // 获取请求的HTTP版本
    let HTTPVersion = httpVersion;
    // 获取请求的方式
    let HTTPType = method;
    // 作者信息对象
    let developer = Config.system.developer;
    // 获取作者名称
    let developerName = developer.name;
    // 获取作者QQ
    let developerQQ = developer.qq;
    // 获取作者微信
    let developerWechat = developer.wechat;
    // 获取作者邮箱
    let developerEmail = developer.email;
    // 获取作者手机号
    let developerMobile = developer.mobile;
    console.log("");
    console.log("------------------------------------------");
    console.log("|            接口请求信息                |");
    console.log("------------------------------------------");
    console.log("|   Type   |  Http", HTTPVersion, "                 |");
    console.log("------------------------------------------");
    console.log("|  Method  |  ", HTTPType);
    console.log("------------------------------------------");
    console.log("|  ", HTTPType, "  |  ", pathname);
    console.log("------------------------------------------");
    console.log("|  ", "Time", "  |  ", dateNow.toLocaleString());
    console.log("------------------------------------------");
    console.log("|  ", "IP ", `|  `, `${ip}`);
    console.log("------------------------------------------");
    console.log("|  ", "IP:v4 ", `|  `, `${IPV4}`);
    console.log("------------------------------------------");
    console.log("|  ", "IP:v6 ", "|  ", `${IPV6}`);
    console.log("------------------------------------------");
    console.log("|  ", "user", "  |  ", `${IPV6}`);
    console.log("------------------------------------------");
    console.log("|               User Agent               |");
    console.log("------------------------------------------");
    console.log(`|   O S    |   ${IPV6}`);
    console.log("------------------------------------------");
    console.log(`|  Device  |   ${IPV6}`);
    console.log("------------------------------------------");
    console.log("|                 Author                 |");
    console.log("------------------------------------------");
    console.log(`|   Name   |   ${developerName}`);
    console.log("------------------------------------------");
    console.log(`|   Q Q    |   ${developerQQ}`);
    console.log("------------------------------------------");
    console.log(`|  Wechat  |   ${developerWechat}`);
    console.log("------------------------------------------");
    console.log(`|  Email   |   ${developerEmail}`);
    console.log("------------------------------------------");
    console.log(`|  Mobile  |   ${developerMobile}`);
    console.log("------------------------------------------");
    console.log("|                 End                    |");
    console.log("------------------------------------------");
    let rquestAgent = request.headers["user-agent"];
    request.$SecretKeyConfig = SecretKeyConfig;
    request.$path = request._parsedUrl.pathname;
    request.$pathname = request._parsedUrl.pathname;
    request.$userAgent = userAgentModule(rquestAgent);
    request.$ip = IPV4;
    request.$ipv4 = IPV4;
    request.$ipv6 = IPV6;
    request.$requestTime = currentTime;
    request.$NowDate = currentTime;
    next();
  } catch (error) {
    console.log(error);
    throw { code: 500, msg: "初始化中间件🤒错误" };
  }
};
